<?php
class Rpp extends CI_Controller {

	function __construct()
	{
		parent::__construct();	
		$this->load->helper('flexigrid');
		$this->load->library('flexigrid');
		$this->load->library('form_validation');
		$this->load->model('master_model');
		$this->load->model('pelajaran_model');
	}
	
	function index($kode_mp,$id_sekolah,$id_ta,$act='')
	{
		$data['value_tingkat'] = '';
		$data['value_kelas'] = '';
		// $data['value_semester'] = '';
		if($act == 'act_filter'){
			$data['value_tingkat'] = $this->input->post('tingkat');
			$data['value_kelas'] = $this->input->post('kelas');
			// $data['value_semester'] = $this->input->post('semester');
			$this->session->set_userdata('sess_rpp_tingkat', $this->input->post('tingkat'));
			$this->session->set_userdata('sess_rpp_kelas', $this->input->post('kelas'));
			// $this->session->set_userdata('sess_rpp_semester', $this->input->post('semester'));
		}else{
			$data['value_tingkat'] = $this->session->userdata('sess_rpp_tingkat');
			$data['value_kelas'] = $this->session->userdata('sess_rpp_kelas');
			// $data['value_semester'] = $this->session->userdata('sess_rpp_semester');
		}
	
		/*
		 * 0 - display name
		 * 1 - width
		 * 2 - sortable
		 * 3 - align
		 * 4 - searchable (2 -> yes and default, 1 -> yes, 0 -> no.)
		 */
		$colModel['no'] = array('No',40,TRUE,'center',0);
		if($this->session->userdata('roleid')=='0') //diknas
			$colModel['NAMA_SEKOLAH'] = array('Nama Sekolah',100,TRUE,'center',1);
		$colModel['TAHUN_AJARAN'] = array('Tahun Ajaran',100,TRUE,'center',1);
		// $colModel['TINGKAT'] = array('Tingkat',100,TRUE,'center',1);
		// $colModel['KELAS'] = array('Kelas',100,TRUE,'center',1);
		$colModel['SEMESTER'] = array('Semester',100,TRUE,'center',1);
		$colModel['KODE_MATA_PELAJARAN'] = array('Mata Pelajaran',100,TRUE,'center',1);
		$colModel['KODE_RPP'] = array('Kode RPP',70,TRUE,'center',1);
		$colModel['MATERI'] = array('Materi',200,TRUE,'center',1);
		$colModel['DESKRIPSI'] = array('Deskripsi',350,TRUE,'center',1);
		$colModel['ubah'] = array('Edit',50,FALSE,'center',0);
		
		/*
		 * Aditional Parameters
		 */
		$gridParams = array(
		'width' => 'auto',
		'height' => '345',
		'rp' => 10,
		'rpOptions' => '[10,15,20,25,40]',
		'pagestat' => 'Displaying: {from} to {to} of {total} items.',
		'blockOpacity' => 0.5,
		'title' => 'Daftar Rencana Program Pembelajaran'
		);
		
		/*
		 * 0 - display name
		 * 1 - bclass
		 * 2 - onpress
		 */
		
		if($act == 'act_filter')
			$buttons[] = array('Add','add','org_js');
		$buttons[] = array('Delete','delete','org_js');
		$buttons[] = array('separator');
		$buttons[] = array('Select All','add','org_js');
		$buttons[] = array('Deselect All','delete','org_js');
		$buttons[] = array('separator');
		$buttons[] = array('Back to Pelajaran','back','org_js');
		
		//Build js
		//View helpers/flexigrid_helper.php for more information about the params on this function
		// if($this->input->post('tingkat')!='' and $this->input->post('kelas')!='' and $this->input->post('semester')!='')
			// $grid_js = build_grid_js('flex1',site_url("/ms_pelajaran_guru/listpelajaran_guru_grid/".$kode_mp."/".$id_sekolah."/".$id_ta."/".$this->input->post('tingkat')."/".$this->input->post('kelas')."/".$this->input->post('semester')),$colModel,'no','asc',$gridParams,$buttons);
		// else
			$grid_js = build_grid_js('flex1',site_url("/rpp/listrpp_grid/".$kode_mp."/".$id_sekolah."/".$id_ta."/".$data['value_tingkat']."/".$data['value_kelas']."/".$this->session->userdata('semester')),$colModel,'no','asc',$gridParams,$buttons);
		
		if($this->session->flashdata('suc_msg'))
			$data['header_info'] = '<div class="ret_success">'.$this->session->flashdata('suc_msg').'</div>';
		
		$data['title'] = "Contoh Flexigrid";
		$data['info'] = "Flexigrid";
		$data['js_grid'] = $grid_js;
		$data['version'] = "0.36";
		$data['download_file'] = "Flexigrid_CI_v0.36.rar";
		
		$data['added_js'] = 
			"<script type='text/javascript'>
			function org_js(com,grid)
			{
				if (com=='Select All')
				{
					$('.bDiv tbody tr',grid).addClass('trSelected');
				}
				
				if (com=='Deselect All')
				{
					$('.bDiv tbody tr',grid).removeClass('trSelected');
				}
				
				if (com=='Add')
				{
					 location.href='".base_url()."index.php/rpp/input_rpp/".$kode_mp."/".$id_sekolah."/".$id_ta."'; 
				}
				
				if (com=='Back to Pelajaran')
				{
					location.href='".base_url()."index.php/ms_pelajaran/'; 
				}
				
				if (com=='Delete')
				{
				   if($('.trSelected',grid).length>0){
					   if(confirm('Anda yakin ingin menghapus ' + $('.trSelected',grid).length + ' buah data?')){
							var items = $('.trSelected',grid);
							var itemlist ='';
							for(i=0;i<items.length;i++){
								itemlist+= items[i].id.substr(3)+',';
							}
							$.ajax({
							   type: 'POST',
							   url: '".site_url('/rpp/delete_rpp')."',
							   data: 'items='+itemlist,
							   success: function(data){
								$('#flex1').flexReload();
								alert(data);
							   }
							});
						}
					} else {
						return false;
					} 
				}          
			} 
			</script>
			";
			
		$data['tingkat'] = $this->master_model->combo_tingkat_kelas($this->session->userdata('unitid'));
		$data['kelas'] = $this->master_model->combo_kelas($this->session->userdata('unitid'),$this->session->userdata('tahunajaran'),$data['value_tingkat']);
		//$data['kelas'] = $this->pelajaran_model->getKelas($id_sekolah,$id_ta);
		// $data['semester'] = $this->pelajaran_model->getSmt($id_sekolah);
		
		$data['url_filter'] = 'rpp/index/'.$kode_mp.'/'.$id_sekolah.'/'.$id_ta.'/act_filter';
		$data['content'] = $this->load->view('grid',$data,true);
		$this->load->view('main',$data);
	}
	
	function listrpp_grid($kode_mp,$id_sekolah,$id_ta,$id_tingkat='',$id_kelas='',$id_smt='')
	{	
	
		//List of all fields that can be sortable. This is Optional.
		//This prevents that a user sorts by a column that we dont want him to access, or that doesnt exist, preventing errors.
		// if($this->session->userdata('roleid')=='0') //diknas
			// $valid_fields = array('NAMA_SEKOLAH','NAMA_TA','KODE_MATA_PELAJARAN','NAMA_MATA_PELAJARAN','KET');
		// else	
			$valid_fields = array('NAMA_TA','TINGKAT','KELAS','SEMESTER','NAMA_MATA_PELAJARAN','KODE_RPP','MATERI','DESKRIPSI');
		
		$this->flexigrid->validate_post('p.KODE_MATA_PELAJARAN','asc',$valid_fields);

		$records = $this->pelajaran_model->get_rpp($kode_mp,$id_sekolah,$id_ta,$id_tingkat,$id_kelas,$id_smt);
		
		// var_dump($records);
		$this->output->set_header($this->config->item('json_header'));
		$no = 0;
		/*
		 * Json build WITH json_encode. If you do not have this function please read
		 * http://flexigrid.eyeviewdesign.com/index.php/flexigrid/example#s3 to know how to use the alternative
		 */
		foreach ($records['records']->result() as $row)
		{
			if($this->session->userdata('roleid')=='0'){ //diknas
				$record_items[] = array($row->ID_RPP,
				$no+1,
				'<div align="left">'.$row->NAMA_TA.'</div>',
				// '<div align="left">'.$row->TINGKAT.'</div>',
				// '<div align="left">'.$row->KELAS.'</div>',
				'<div align="left">'.$row->SEMESTER.'</div>',
				'<div align="left">'.$row->NAMA_MATA_PELAJARAN.'</div>',
				'<div align="left">'.$row->KODE_RPP.'</div>',
				'<div align="left">'.$row->MATERI.'</div>',
				'<div align="left">'.$row->DESKRIPSI.'</div>',
				'<a href=\''.site_url().'/rpp/edit_rpp/'.$kode_mp.'/'.$id_sekolah.'/'.$id_ta.'/'.$row->ID_RPP.'/\'><img border=\'0\' src=\''.base_url().'images/main/28.png\'></a> '
				);
				$no++;
			}else{
				$record_items[] = array($row->ID_RPP,
				$no+1,
				'<div align="left">'.$row->NAMA_TA.'</div>',
				// '<div align="left">'.$row->TINGKAT.'</div>',
				// '<div align="left">'.$row->KELAS.'</div>',
				'<div align="left">'.$row->SEMESTER.'</div>',
				'<div align="left">'.$row->NAMA_MATA_PELAJARAN.'</div>',
				'<div align="left">'.$row->KODE_RPP.'</div>',
				'<div align="left">'.$row->MATERI.'</div>',
				'<div align="left">'.$row->DESKRIPSI.'</div>',
				'<a href=\''.site_url().'/rpp/edit_rpp/'.$kode_mp.'/'.$id_sekolah.'/'.$id_ta.'/'.$row->ID_RPP.'/\'><img border=\'0\' src=\''.base_url().'images/main/28.png\'></a> '
				);
				$no++;
			}
		}
		//Print please
		if(isset($record_items))
				$this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
			else
				$this->output->set_output('{"page":"1","total":"0","rows":[]}');
	}
	
	function cek_validasi() {	
			// Setting Rules
			$this->form_validation->set_rules('tingkat', 'Tingkat', 'required');
			$this->form_validation->set_rules('kelas', 'Kelas', 'required');
			// $this->form_validation->set_rules('semester', 'Semester', 'required');
			$this->form_validation->set_rules('kode_mp', 'Mata Pelajaran', 'required');
			$this->form_validation->set_rules('kode_rpp', 'Kode Rpp', 'required');
			if($this->session->userdata('roleid')=='0') //diknas
				$this->form_validation->set_rules('nama_sekolah', 'Nama Sekolah', 'required');
			
			// Setting Error Message
			$this->form_validation->set_message('required', 'Kolom %s harus diisi!!');
			
			// Setting Delimiter
			$this->form_validation->set_error_delimiters('<li class="error">', '</li>');
			return $this->form_validation->run();
	}
	
	function cek_rpp($kode_mp,$id_sekolah,$thn_ajaran,$tingkat, $kelas, $smt, $kode_rpp){
		$rs_cek = $this->pelajaran_model->validasi_rpp($kode_mp,$id_sekolah,$thn_ajaran, $tingkat, $kelas, $smt, $kode_rpp)->row();
		return $rs_cek;
	}
	
	function input_rpp($kode_mp,$id_sekolah,$id_ta)
    {
		if ($this->cek_validasi() == FALSE){
			$data['status'] = 'new';
			$data['param_id'] = $kode_mp."#".$id_sekolah."#".$id_ta;
			$data['tahunajaran'] = $this->pelajaran_model->getTa($id_sekolah);
			$data['pelajaran'] = $this->pelajaran_model->getMp($id_sekolah,$kode_mp);
			$data['r_tingkat'] = $this->pelajaran_model->getTingkat($id_sekolah);
			$data['r_kelas'] = $this->pelajaran_model->getKelas($id_sekolah);
			$data['r_semester'] = $this->pelajaran_model->getSmt($id_sekolah);
			
			$data['content'] = $this->load->view('form_rpp',$data,true);
			$this->load->view('main',$data);
		}
		else{
			if($this->cek_rpp($kode_mp,$id_sekolah,$id_ta,$this->input->post('tingkat'),$this->input->post('kelas'),$this->session->userdata('semester'), $this->input->post('kode_rpp'))->jml > 0){ //sdh ada di database, reject
				$data['status'] = 'new';
				$data['param_id'] = $kode_mp."#".$id_sekolah."#".$id_ta;
				// $data['sekolah'] = $id_sekolah;
				$data['tahunajaran'] = $this->pelajaran_model->getTa($id_sekolah);
				$data['pelajaran'] = $this->pelajaran_model->getMp($id_sekolah,$kode_mp);
				$data['r_tingkat'] = $this->pelajaran_model->getTingkat($id_sekolah);
				$data['r_kelas'] = $this->pelajaran_model->getKelas($id_sekolah);
				$data['r_semester'] = $this->pelajaran_model->getSmt($id_sekolah);
				
				$data['value'] = $this->input->post();
				$data['err_msg'] = 'Kode RPP tersebut telah ada dalam database.';
				$data['content'] = $this->load->view('form_rpp',$data,true);
				$this->load->view('main',$data);
			}else
				$this->insert_rpp($kode_mp,$id_sekolah,$id_ta);
		}            
    }
	
	function insert_rpp($kode_mp,$id_sekolah,$id_ta){
		// if($this->session->userdata('roleid')=='0'){ //diknas
			// $nama_sekolah = $this->input->post('nama_sekolah');
		// }else{
			// // $nama_sekolah = $this->session->userdata('unitid');
			// $nama_sekolah = $id_sekolah;
		// }
		// $id_pengajar = $this->pelajaran_model->getLastPengajar()->row();
		$data = array(
			'TAHUN_AJARAN'			=> $id_ta,
			'ID_SEKOLAH'			=> $id_sekolah,
			'KODE_MATA_PELAJARAN'	=> $kode_mp,	
			'ID_TINGKAT'			=> $this->input->post('tingkat'),	
			'ID_KELAS'				=> $this->input->post('kelas'),	
			'ID_SEMESTER'			=> $this->session->userdata('semester'),	
			'KODE_RPP'				=> $this->input->post('kode_rpp'),	
			'MATERI'	 			=> $this->input->post('materi'),
			'DESKRIPSI'	 			=> $this->input->post('deskripsi')
		);
		$this->pelajaran_model->insert_rpp($data);
		$this->session->set_flashdata('suc_msg', 'Data berhasil disimpan.');
		redirect('rpp/index/'.$kode_mp.'/'.$id_sekolah.'/'.$id_ta); 
	}
	
	function edit_rpp($kode_mp,$id_sekolah,$id_ta,$id_rpp)
    {
 		if ($this->cek_validasi() == FALSE){
			$data['status'] = 'edit';
			$data['param_id'] = $kode_mp."#".$id_sekolah."#".$id_ta;
			$data['tahunajaran'] = $this->pelajaran_model->getTa($id_sekolah);
			$data['pelajaran'] = $this->pelajaran_model->getMp($id_sekolah,$kode_mp);
			$data['r_tingkat'] = $this->pelajaran_model->getTingkat($id_sekolah);
			$data['r_kelas'] = $this->pelajaran_model->getKelas($id_sekolah);
			$data['r_semester'] = $this->pelajaran_model->getSmt($id_sekolah);
			
			$data['rs_rpp'] = $this->pelajaran_model->get_rppID($id_rpp);
			$data['content'] = $this->load->view('form_rpp',$data,true);
			$this->load->view('main',$data);
		}
		else{
			if($this->cek_rpp($this->input->post('kode_mp'),$this->input->post('tahun_ajaran'),$this->session->userdata('unitid'),$this->input->post('tingkat'),$this->input->post('kelas'),$this->session->userdata('semester'),$this->input->post('kode_rpp'))->jml > 0 and $this->cek_pelajaran_guru($this->input->post('kode_mp'),$this->input->post('tahun_ajaran'),$this->session->userdata('unitid'),$this->input->post('tingkat'),$this->input->post('kelas'),$this->session->userdata('semester'),$this->input->post('kode_rpp'))->ID_RPP != $id_rpp){
				$data['status'] = 'edit';
				$data['param_id'] = $kode_mp."#".$id_sekolah."#".$id_ta;
				$data['tahunajaran'] = $this->pelajaran_model->getTa($id_sekolah);
				$data['pelajaran'] = $this->pelajaran_model->getMp($id_sekolah,$kode_mp);
				$data['r_tingkat'] = $this->pelajaran_model->getTingkat($id_sekolah);
				$data['r_kelas'] = $this->pelajaran_model->getKelas($id_sekolah);
				// $data['r_semester'] = $this->pelajaran_model->getSmt($id_sekolah);
				
				$data['rs_rpp'] = $this->pelajaran_model->get_rppID($id_pg);
				$data['err_msg'] = 'Kode RPP telah ada dalam database.';
				$data['content'] = $this->load->view('form_pelajaran_guru',$data,true);
				$this->load->view('main',$data);
			}else
				$this->update_rpp($kode_mp,$id_sekolah,$id_ta,$id_rpp);
		}            
    }
	
	function update_rpp($kode_mp,$id_sekolah,$id_ta,$id_rpp){
		// if($this->session->userdata('roleid')=='0'){ //diknas
			// $nama_sekolah = $this->input->post('nama_sekolah');
		// }else{
			// $nama_sekolah = $nama_sekolahp;
		// }
		
		$data = array(
			'KODE_RPP'				=> $this->input->post('kode_rpp'),	
			'MATERI'				=> $this->input->post('materi'),	
			'DESKRIPSI'				=> $this->input->post('deskripsi')
		);
		
		$this->pelajaran_model->update_rpp($data, $id_rpp);
		$this->session->set_flashdata('suc_msg', 'Data berhasil diupdate.');
		redirect('rpp/index/'.$kode_mp.'/'.$id_sekolah.'/'.$id_ta); 
	}
	
	//Delete
	function delete_rpp()
	{
		$ta_ids_post_array = explode(",",$this->input->post('items'));
		$msg = '';
		foreach($ta_ids_post_array as $index => $ta_id){
			if (isset($ta_id) && $ta_id != "") {
				$this->pelajaran_model->delete_rpp($ta_id);
				$msg .= "Data RPP berhasil dihapus.\n";
			}
		}
		$this->output->set_header($this->config->item('ajax_header'));
		$this->output->set_output($msg);
	}
	
	function filter($kode_mp,$id_sekolah,$id_ta){
		$this->session->set_flashdata('suc_msg', 'Data berhasil disimpan22.');
		redirect('rpp/index/'.$kode_mp.'/'.$id_sekolah.'/'.$id_ta); 
	}
    
}
?>
